package com.alstudio.utils.android;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Locale;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

public class AndroidUtils {

	private static AirplaneModeChangedCallback mAirplaneModeChangedCallback;

	public static ConnectivityManager getConnectivityManager(Context ctx) {
		return (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	public static WifiManager getWifiManager(Context ctx) {
		return (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
	}

	public static TelephonyManager getTelephonyManager(Context ctx) {
		return (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
	}

	public static LocationManager getLocationManager(Context ctx) {
		return (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
	}

	/**
	 * 检测当前是否有可用数据网络
	 * 
	 * @param context
	 * @return true表示当前有可用数据网络
	 */
	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		if (activeNetworkInfo != null) {
			return activeNetworkInfo.isConnected();
		}
		return false;
	}

	/**
	 * 检测当前wifi网络是否可用
	 * 
	 * @param context
	 * @return true表示当前wifi网络已连接
	 */
	public static boolean isWifiConnected(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiNetworkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetworkInfo != null) {
			return wifiNetworkInfo.isConnected();
		}
		return false;
	}

	/**
	 * 获取手机设备名
	 * 
	 * @return
	 */
	public static String getDeviceModel() {
		return android.os.Build.MODEL;
	}

	/**
	 * 获取系统sdk版本号
	 * 
	 * @return
	 */
	public static String getSystemSdkVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	public static int getSystemSdkVersionCode() {
		return android.os.Build.VERSION.SDK_INT;
	}

	/**
	 * 获取系统当前语言
	 * 
	 * @return
	 */
	public static String getSystemLanguage() {
		return Locale.getDefault().getLanguage();
	}

	/**
	 * 获取系统当前国家位置设定信息
	 * 
	 * @return
	 */
	public static String getCountry() {
		return Locale.getDefault().getCountry();
	}

	/**
	 * 切换飞行模式
	 * 
	 * @param context
	 */
	public static void toggleAirPlaneMode(Context context) {

		boolean enabled = !getAirplaneMode(context);
		Intent intent;
		Settings.System.putInt(context.getContentResolver(),
				Settings.System.AIRPLANE_MODE_ON, enabled ? 1 : 0);
		intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", enabled);
		context.sendBroadcast(intent);
	}

	/**
	 * 切换飞行模式
	 * 
	 * @param context
	 */
	public static void toggleAirPlaneMode(Context context,
			AirplaneModeChangedCallback callback) {

		boolean enabled = !getAirplaneMode(context);
		Intent intent;
		Settings.System.putInt(context.getContentResolver(),
				Settings.System.AIRPLANE_MODE_ON, enabled ? 1 : 0);
		intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", enabled);
		context.sendBroadcast(intent);
		mAirplaneModeChangedCallback = callback;
		if (mAirplaneModeChangedCallback != null) {
			mAirplaneModeChangedCallback.onAirplaneModeChanged(enabled);
		}
	}

	/**
	 * 判断手机是否是飞行模式
	 * 
	 * @param context
	 * @return
	 */
	public static boolean getAirplaneMode(Context context) {
		int isAirplaneMode = Settings.System.getInt(
				context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
				0);
		return (isAirplaneMode == 1) ? true : false;
	}

	/**
	 * 应用程序运行命令获取 Root权限，设备必须已破解(获得ROOT权限)
	 * 
	 * @return 应用程序是/否获取Root权限
	 */
	public static boolean getRootPermission(Context context) {
		Process process = null;
		DataOutputStream os = null;

		if (context == null) {
			return false;
		}

		String pkgCodePath = context.getPackageCodePath();

		try {
			String cmd = "chmod 777 " + pkgCodePath;
			process = Runtime.getRuntime().exec("su"); // 切换到root帐号
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}
		return true;
	}

	/**
	 * 重启手机，设备必须已破解(获得ROOT权限)
	 */
	public static void rebootDevice() {

		String cmd = "reboot -p";
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
	}

	/**
	 * 关闭手机
	 */
	public static void powerOffDevice() {

		String cmd = "su -c shutdown";
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
	}

	/**
	 * 设置使用模式
	 * 
	 * @param ctx
	 * @param mode
	 */
	public static void setRingerMode(Context ctx, RingerMode mode) {
		AudioManager am = (AudioManager) ctx
				.getSystemService(Context.AUDIO_SERVICE);
		// 得到AudioManager对象
		switch (mode) {
		case RINGER_MODE_NORMAL:
			// 正常模式,有声,是否震动取决于原来系统声音设置中振动的设置
			am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			break;
		case RINGER_MODE_SILENT:
			// 静音模式,无声不震
			am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			break;
		case RINGER_MODE_VIBRATE:
			// 震动模式,无声,震动
			am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			break;
		}
	}

	public static RingerMode getRingerMode(Context ctx) {

		AudioManager am = (AudioManager) ctx
				.getSystemService(Context.AUDIO_SERVICE);
		// 得到AudioManager对象
		int mode = am.getRingerMode();
		RingerMode ringMode = RingerMode.RINGER_MODE_NORMAL;
		switch (mode) {
		case AudioManager.RINGER_MODE_NORMAL:
			// 正常模式,有声,是否震动取决于原来系统声音设置中振动的设置
			ringMode = RingerMode.RINGER_MODE_NORMAL;
			break;
		case AudioManager.RINGER_MODE_SILENT:
			// 静音模式,无声不震
			ringMode = RingerMode.RINGER_MODE_SILENT;
			break;
		case AudioManager.RINGER_MODE_VIBRATE:
			// 震动模式,无声,震动
			ringMode = RingerMode.RINGER_MODE_VIBRATE;
			break;
		}
		return ringMode;
	}

	public enum RingerMode {
		// 正常模式,有声,是否震动取决于原来系统声音设置中振动的设置
		RINGER_MODE_NORMAL,
		// 静音模式,无声不震
		RINGER_MODE_SILENT,
		// 震动模式,无声,震动
		RINGER_MODE_VIBRATE,
	}

	/**
	 * 安装应用包
	 * 
	 * @param context
	 * @param path
	 */
	public static void installApk(Context context, String path) {
		if (ALFileManager.isFileExsit(path)) {
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.setDataAndType(Uri.parse("file://" + path),
					"application/vnd.android.package-archive");
			context.startActivity(i);
		}
	}

	/**
	 * 跳转到google play评价软件
	 * 
	 * @param context
	 */
	public static void rateApplication(Context context) {
		Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
		Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
		try {
			context.startActivity(goToMarket);
		} catch (ActivityNotFoundException e) {
			context.startActivity(new Intent(Intent.ACTION_VIEW, Uri
					.parse("http://play.google.com/store/apps/details?id="
							+ context.getPackageName())));
		}
	}

	public interface AirplaneModeChangedCallback {
		public void onAirplaneModeChanged(boolean state);
	}
}
